# from ultralytics import YOLO

# model = YOLO('best_openvino_model/',task='detect')
# # model.fuse()
# results = model(source=0, show=True, conf=0.5, save=False, verbose=False, stream=True, device='cpu')
# while True:
#     # print("1")
#     # print("2")
#     try:
#         result = next(results)
#         while result is not None:
#             # 检查是否检测到了目标
#             if result.boxes is not None and len(result.boxes.cpu().numpy()) > 0:
#                 for box in result.boxes.xyxy:
#                     # 检查边界框的长度是否为 4
#                     if len(box) == 4:
#                         # print("归一化的 xy 坐标:", box.tolist())  # 只打印归一化的 xy 坐标
#                         # 替换原有的获取图像宽度和高度的代码
#                         W, H = result.orig_img.shape[1], result.orig_img.shape[0]  # 获取图像的宽度和高度

#                         # 计算中心点的归一化坐标
#                         center_x = (box[0] + box[2]) / (2 * W)
#                         center_y = (box[1] + box[3]) / (2 * H)
#                         # print("中心点的归一化坐标:", [center_x, center_y])
#                         print("中心点的归一化坐标:", center_x.item(), center_y.item())
#                     else:
#                         print("边界框格式不正确:", box)
#             else:
#                 print("No Target")
#             result = next(results)
#     except StopIteration:
#         pass


from ultralytics import YOLO
import cv2

model = YOLO('weights/best_7_openvino_model/',task='detect')
# model.fuse()

results = model(source=0, show=True, conf=0.7, save=False, verbose=False, stream=True, device='cpu')
while True:
    # print("1")
    # print("2")
    try:
        result = next(results)
        while result is not None:
            # 检查是否检测到了目标
            if result.boxes is not None and len(result.boxes.cpu().numpy()) > 0:
                boxes = result.boxes.cpu().numpy()
                # x y 归一化坐标
                normalized_xy = boxes.xywhn[:, :2]
                # print("Normalized XY coordinates:", normalized_xy)
                x = normalized_xy[0][0]
                y = normalized_xy[0][1]
                # print(x)
                # print(y)
                classes = boxes.cls
                name = classes[0]
                # print("Classes:", name)
                yolo_list = [name,x,y]
                print(yolo_list)
            else:
                print("No Target")
            result = next(results)
    except StopIteration:
        pass


# from ultralytics import YOLO
# import cv2

# # 定义调整对比度的函数
# def adjust_contrast(frame, alpha=1.0, beta=0):
#     """
#     调整图像的对比度。
#     :param frame: 输入的图像帧。
#     :param alpha: 用于调整对比度的权重。
#     :param beta: 用于调整对比度的偏置。
#     :return: 对比度调整后的图像帧。
#     """
#     return cv2.addWeighted(frame, alpha, frame, 0, beta)

# model = YOLO('weights/best_5_openvino_model/', task='detect')
# # model.fuse()

# # 读取视频流
# cap = cv2.VideoCapture(0)

# while True:
#     ret, frame = cap.read()
#     if not ret:
#         break

#     # 在此处调整对比度
#     adjusted_frame = adjust_contrast(frame, alpha=2.0, beta=50)

#     # 将调整后的帧传递给 YOLO 模型
#     results = model(source=adjusted_frame, show=True, conf=0.7, save=False, verbose=False, stream=True, device='cuda')

#     try:
#         result = next(results)
#         while result is not None:
#             # 检查是否检测到了目标
#             if result.boxes is not None and len(result.boxes.cpu().numpy()) > 0:
#                 boxes = result.boxes.cpu().numpy()
#                 # x y 归一化坐标
#                 normalized_xy = boxes.xywhn[:, :2]
#                 x = normalized_xy[0][0]
#                 y = normalized_xy[0][1]
#                 # classes = boxes.cls
#                 # name = classes[0]
#                 # yolo_list = [name, x, y]
#                 # print(yolo_list)
#                 print("Detected object at:", (x, y))
#             else:
#                 print("No Target")
#             result = next(results)
#     except StopIteration:
#         pass

# cap.release()
# cv2.destroyAllWindows()